package com.kedacom.freedomLabel.view.fragment.setting;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Settings;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.kedacom.freedomLabel.R;
import com.kedacom.freedomLabel.databinding.FragmentSystemSettingBinding;
import com.kedacom.freedomLabel.view.activity.MainActivity;
import com.kedacom.freedomLabel.viewmodel.SystemSettingViewModel;
import com.kedacom.lego.annotation.ContentView;
import com.kedacom.lego.annotation.ViewModel;
import com.kedacom.lego.util.Utils;
import com.lzscale.scalelib.misclib.Misc;
import com.seray.scaleviewlib.callback.IPositiveClickListener;
import com.seray.scaleviewlib.dialog.ScaleTipDialog;
import com.seray.scaleviewlib.utils.ToastUtils;

import java.text.SimpleDateFormat;
import java.util.Date;

import static android.service.notification.Condition.SCHEME;
import static com.kedacom.freedomLabel.utils.FileHelp.deleteCalibrationFile;


@ContentView(R.layout.fragment_system_setting)
@ViewModel(SystemSettingViewModel.class)
public class SystemSettingFragment extends ASettingFragment<FragmentSystemSettingBinding, SystemSettingViewModel> {

    @Override
    public void saveData() {

    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mMisc = Misc.newInstance();
    }

    @Override
    public String getFragmentTitle() {
        return Utils.getApp().getString(R.string.setting_frag_name_system);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View root = super.onCreateView(inflater, container, savedInstanceState);
        mBinding.systemSettingTouchScreen.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                    android.app.AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
                    builder.setTitle("警告框");
                    builder.setMessage(R.string.dialog_shutdown_msg);
                    builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            if (deleteCalibrationFile()) {
                                ToastUtils.showShort(R.string.dialog_msg_delete);
                                shutDown();
                            }else {
                                ToastUtils.showShort("删除出错");
                            }
                        }
                    });
                    builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                        }
                    });
                    builder.show();
                }
        });
        Date dd=new Date();
        SimpleDateFormat sim=new SimpleDateFormat("yyyy年MM月dd日 HH:mm");
        String time=sim.format(dd);
        mBinding.systemSettingDate.setText(time);
        mBinding.systemSettingDate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getActivity().startActivity(new Intent(Settings.ACTION_DATE_SETTINGS));
            }
        });



        mBinding.systemSetting.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Settings.ACTION_SETTINGS);
                startActivity(intent);
            }
        });
        mBinding.systemWlan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Settings.ACTION_WIFI_SETTINGS);
                startActivity(intent);

            }
        });
        return root;
    }

    /**
     * 关机操作
     */
    public void shutDown() {
        Intent shutDownIntent = new Intent("android.intent.action.ACTION_REQUEST_SHUTDOWN");
        shutDownIntent.putExtra("android.intent.extra.KEY_CONFIRM", true);
        shutDownIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(shutDownIntent);
    }
}